cmake_minimum_required(VERSION 3.16)

project(lasrwebsocket) 

option(ENABLE_WEBSOCKET "ws" ON)
option(ENABLE_PORTAUDIO "portaudio" ON)
option(ENABLE_GLOG "Whether to build glog" ON)

if(ENABLE_GLOG)
    include_directories(${PROJECT_SOURCE_DIR}/onnxruntime/third_party/gflags)
    add_subdirectory(${PROJECT_SOURCE_DIR}/onnxruntime/third_party/gflags gflags)
    include_directories(${gflags_BINARY_DIR}/include)


    include_directories(${PROJECT_SOURCE_DIR}/onnxruntime/third_party/glog/src)
    set(BUILD_TESTING OFF)
    add_subdirectory(${PROJECT_SOURCE_DIR}/onnxruntime/third_party/glog glog)
    include_directories(${glog_BINARY_DIR})

endif()

if(ENABLE_WEBSOCKET)
  # cmake_policy(SET CMP0135 NEW)
  include(FetchContent)

  if(NOT EXISTS ${PROJECT_SOURCE_DIR}/third_party/websocket/websocketpp )
    FetchContent_Declare(websocketpp
    GIT_REPOSITORY https://github.com/zaphoyd/websocketpp.git
      GIT_TAG 0.8.2
      SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/websocket
      )
    
    FetchContent_MakeAvailable(websocketpp)
  endif()
  include_directories(${PROJECT_SOURCE_DIR}/third_party/websocket)
   
  if(NOT EXISTS ${PROJECT_SOURCE_DIR}/third_party/asio/asio )
    FetchContent_Declare(asio
      URL   https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-24-0.tar.gz
    SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/asio
    )
    
    FetchContent_MakeAvailable(asio)
  endif()
  include_directories(${PROJECT_SOURCE_DIR}/third_party/asio/asio/include)
 
  if(NOT EXISTS ${PROJECT_SOURCE_DIR}/third_party/json/ChangeLog.md )
    FetchContent_Declare(json
      URL   https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.tar.gz
    SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/json
    )
    
    FetchContent_MakeAvailable(json)
  endif()
  include_directories(${PROJECT_SOURCE_DIR}/third_party/json/include)

  
  if(NOT EXISTS ${PROJECT_SOURCE_DIR}/third_party/glog/src )
    FetchContent_Declare(glog
      URL  https://github.com/google/glog/archive/refs/tags/v0.7.0.tar.gz
    SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/glog/src
    )
    
    FetchContent_MakeAvailable(glog)
  endif()
  include_directories(${PROJECT_SOURCE_DIR}/third_party/glog/include)
endif()



if(ENABLE_PORTAUDIO)
  include(FetchContent)

  set(portaudio_URL  "http://files.portaudio.com/archives/pa_stable_v190700_20210406.tgz")
  set(portaudio_URL2 "https://isv-data.oss-cn-hangzhou.aliyuncs.com/ics/MaaS/ASR/dep_libs/pa_stable_v190700_20210406.tgz")
  set(portaudio_HASH "SHA256=47efbf42c77c19a05d22e627d42873e991ec0c1357219c0d74ce6a2948cb2def")

  FetchContent_Declare(portaudio
    URL
      ${portaudio_URL}
      ${portaudio_URL2}
    URL_HASH          ${portaudio_HASH}
  )

  FetchContent_GetProperties(portaudio)
  if(NOT portaudio_POPULATED)
    message(STATUS "Downloading portaudio from ${portaudio_URL}")
    FetchContent_Populate(portaudio)
  endif()
  message(STATUS "portaudio is downloaded to ${portaudio_SOURCE_DIR}")
  message(STATUS "portaudio's binary dir is ${portaudio_BINARY_DIR}")

  add_subdirectory(${portaudio_SOURCE_DIR} ${portaudio_BINARY_DIR} EXCLUDE_FROM_ALL)
  if(NOT WIN32)
    target_compile_options(portaudio PRIVATE "-Wno-deprecated-declarations")
  else()
    install(TARGETS portaudio DESTINATION ..)
  endif()

endif()


find_package(OpenSSL REQUIRED)

message("CXX_FLAGS "${CMAKE_CXX_FLAGS})
# 获取项目中所有包含文件夹的路径
get_property(includes DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
# 遍历并输出每个包含文件夹的路径
foreach(include ${includes})
  message("Include directory: ${include}")
endforeach()



add_executable(lasr-wss-client "lasr-wss-client.cpp" ${RELATION_SOURCE})
add_executable(lasr-wss-client-2pass "lasr-wss-client-2pass.cpp" "microphone.cpp" ${RELATION_SOURCE})
target_link_libraries(lasr-wss-client PUBLIC lasr ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY})
target_link_libraries(lasr-wss-client-2pass PUBLIC lasr ${OPENSSL_CRYPTO_LIBRARY} ${OPENSSL_SSL_LIBRARY} portaudio)

